import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Demo_BlockingQueue {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(1000);
        Thread t1 = new Thread(() -> {
            while(true){
                try {
                    String ret = blockingQueue.take();
                    System.out.println("consumer:" + ret);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "consumer");
        Thread t2 = new Thread(() -> {
            int count = 1;
            while(true){
                try {
                    blockingQueue.put("" + count);
                    System.out.println("producer:" + count);

                    count++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }, "producer");

        t1.start();
        t2.start();
    }
}